package com.diandian.backend.order.service;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.diandian.backend.order.domain.OrderOnlineDouyinLifeDetail;
import com.diandian.backend.order.dto.OrderOnlineDouyinLifeDetailAddParamDTO;
import com.diandian.backend.order.dto.OrderOnlineDouyinLifeDetailDetailParamDTO;
import com.diandian.backend.order.dto.OrderOnlineDouyinLifeDetailListParamDTO;
import com.diandian.backend.order.dto.OrderOnlineDouyinLifeDetailPageParamDTO;
import com.diandian.backend.order.mapper.OrderOnlineDouyinLifeDetailMapper;
import com.diandian.backend.order.resp.OrderOnlineDouyinLifeDetailDetailResp;
import com.diandian.backend.order.resp.OrderOnlineDouyinLifeDetailListResp;
import com.diandian.backend.order.resp.OrderOnlineDouyinLifeDetailPageResp;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderOnlineDouyinLifeDetailService {

    @Autowired
    private OrderOnlineDouyinLifeDetailMapper orderOnlineDouyinLifeDetailMapper;

    public PageResult page(OrderOnlineDouyinLifeDetailPageParamDTO requestDTO){

        requestDTO.check();
        int pageSize = requestDTO.getPageSize();
        int pageNum = requestDTO.getPageNum();

        // 查询总数
        int totalCount = orderOnlineDouyinLifeDetailMapper.getCount(requestDTO);

        // 查询数据集合
        List<OrderOnlineDouyinLifeDetail> dataList = orderOnlineDouyinLifeDetailMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<OrderOnlineDouyinLifeDetailPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                OrderOnlineDouyinLifeDetailPageResp item = new OrderOnlineDouyinLifeDetailPageResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setOnlineOrderNum(x.getOnlineOrderNum());
                item.setOnlineOrderCouponId(x.getOnlineOrderCouponId());
                item.setOnlineOrderCouponName(x.getOnlineOrderCouponName());
                item.setCreateTime(x.getCreateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<OrderOnlineDouyinLifeDetailListResp> getList(OrderOnlineDouyinLifeDetailListParamDTO requestDTO){

        // 查询数据集合
        List<OrderOnlineDouyinLifeDetail> dataList = orderOnlineDouyinLifeDetailMapper.getList(requestDTO);

        // 组装数据
        List<OrderOnlineDouyinLifeDetailListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                OrderOnlineDouyinLifeDetailListResp item = new OrderOnlineDouyinLifeDetailListResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setOnlineOrderNum(x.getOnlineOrderNum());
                item.setOnlineOrderCouponId(x.getOnlineOrderCouponId());
                item.setOnlineOrderCouponName(x.getOnlineOrderCouponName());
                item.setCreateTime(x.getCreateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public OrderOnlineDouyinLifeDetailDetailResp getDetail(OrderOnlineDouyinLifeDetailDetailParamDTO param) {

        OrderOnlineDouyinLifeDetail dbData = orderOnlineDouyinLifeDetailMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        OrderOnlineDouyinLifeDetailDetailResp result = new OrderOnlineDouyinLifeDetailDetailResp();
        result.setId(dbData.getId());
        result.setBelong(dbData.getBelong());
        result.setOnlineOrderNum(dbData.getOnlineOrderNum());
        result.setOnlineOrderCouponId(dbData.getOnlineOrderCouponId());
        result.setOnlineOrderCouponName(dbData.getOnlineOrderCouponName());
        result.setCreateTime(dbData.getCreateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    /**
     * 删除客户端下某个时间段内的订单数据
     * @param belong
     * @param startTime
     * @param endTime
     */
    public void deleteByTime(String belong, Date startTime, Date endTime) {
        orderOnlineDouyinLifeDetailMapper.deleteByTime(belong, startTime, endTime);
    }

    public void deleteByOrderNumAndBelong(String belong, String onlineOrderNum) {
        orderOnlineDouyinLifeDetailMapper.deleteByOrderNumAndBelong(belong, onlineOrderNum);
    }

    /**
     * 批量保存订单数据
     * @param dataList
     */
    public void batchAdd(List<OrderOnlineDouyinLifeDetailAddParamDTO> dataList) {
        if(dataList == null || dataList.isEmpty()) {
            return;
        }

        dataList.forEach( x -> {
            x.setDel(0);
        });

        List<List<OrderOnlineDouyinLifeDetailAddParamDTO>> partitionList = Lists.partition(dataList,100);
        for (int i = 0; i < partitionList.size(); i++) {
            orderOnlineDouyinLifeDetailMapper.batchAdd(partitionList.get(i));
        }
    }
}
